package com.pie4cloud.pie.job.biz.gather.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pie4cloud.pie.common.core.util.R;
import com.pie4cloud.pie.common.log.annotation.SysLog;
import com.pie4cloud.pie.job.api.entity.ConfigDecode;
import com.pie4cloud.pie.job.api.utils.RegException;
import com.pie4cloud.pie.job.api.utils.RegUtils;
import com.pie4cloud.pie.job.biz.gather.service.ConfigDecodeService;
import com.pie4cloud.pie.job.biz.service.PlayBillService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 
 *
 * @author feilei
 * @date 2022-01-10 00:54:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/configdecode" )
@Api(value = "configdecode", tags = "管理")
public class ConfigDecodeController {

    private final ConfigDecodeService configDecodeService;

    private final PlayBillService playBillService;


    /**
     * 分页查询
     * @param page 分页对象
     * @param configDecode 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('gather_configdecode_get')" )
    public R getConfigDecodePage(Page page, ConfigDecode configDecode) {
        return R.ok(configDecodeService.page(page, Wrappers.query(configDecode)));
    }

	/**
	 * 查询所有
	 * @return
	 */
	@ApiOperation(value = "查询所有", notes = "查询所有")
	@GetMapping("/list" )
	@PreAuthorize("@pms.hasPermission('gather_configdecode_get')" )
	public R getAllConfigDecode() {
		return R.ok(configDecodeService.list());
	}


	/**
	 * 样例文件解码
	 * @return
	 */
	@ApiOperation(value = "样例文件解码", notes = "样例文件解码")
	@GetMapping("/sampleFileDecode" )
	@PreAuthorize("@pms.hasPermission('gather_configdecode_get')" )
	public R sampleFileDecode(String fileName,String pathReg) {
		try {
			return R.ok(RegUtils.matchToMap(pathReg,fileName));
		} catch (RegException e) {
			return R.failed("解码匹配失败，请查验正则与样例文件");
		}
	}


    /**
     * 通过id查询
     * @param decodeId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{decodeId}" )
    @PreAuthorize("@pms.hasPermission('gather_configdecode_get')" )
    public R getById(@PathVariable("decodeId" ) Long decodeId) {
        return R.ok(configDecodeService.getById(decodeId));
    }

    /**
     * 新增
     * @param configDecode 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('gather_configdecode_add')" )
    public R save(@RequestBody ConfigDecode configDecode) {
		boolean save = configDecodeService.save(configDecode);
		if (save) {
			return R.ok(configDecode);
		}else {
			return R.failed("保存解码信息失败");
		}
    }

    /**
     * 修改
     * @param configDecode 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('gather_configdecode_edit')" )
    public R updateById(@RequestBody ConfigDecode configDecode) {
        return R.ok(configDecodeService.updateById(configDecode));
    }

    /**
     * 通过id删除
     * @param decodeId id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{decodeId}" )
    @PreAuthorize("@pms.hasPermission('gather_configdecode_del')" )
    public R removeById(@PathVariable Long decodeId) {
        return R.ok(configDecodeService.removeById(decodeId));
    }

}
